home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / MailEnclosure / MESendModules / Source / NeXTMail / NeXTMail.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.3 KB  |  132 lines

  1. #import "NeXTMail.h"
  2. #import "StringStorage.h"
  3. #import "EnhancedApp.h"
  4. #import "FileName.h"
  5. #import "support.h"
  6. #include <sys/file.h>
  7. #include <sys/types.h>
  8. #include <sys/uio.h>
  9. #include <sys/stat.h>
  10.  
  11. const char rtfHeader[] = "{\\rtf0\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\n";
  12. const char templateAttach[] = "{{\\attachment0 %s\n}\n";
  13. const char tarCmd[] = "tar chf - -C %s * | compress > %s";
  14. const char envelopeAddition[] = "NeXT-Attachment: %s, %d, 1/1, 9999, 0\n";
  15.    
  16.  
  17. @implementation NeXTMail
  18.  
  19. -initForMUA: (const char *)anApp  andMTA: (const char *)aMailer
  20. {
  21.    [super initForMUA: anApp andMTA: aMailer];
  22.    tmpDir = [[StringStorage alloc] init:"/tmp/.nextmail"];
  23.    [tmpDir mktemp];
  24.    mkdir([tmpDir stringValue],0700);
  25.    attachFile = [[StringStorage alloc] init: [tmpDir stringValue]];
  26.    [attachFile appendStringValue: ".attach"];
  27.    chdir([tmpDir stringValue]);
  28.  
  29.    [mailFile setStringValue: "/tmp/.mail"];
  30.    [mailFile mktemp];
  31.    fp = fopen([mailFile stringValue], "a");
  32.    if(!fp)
  33.        NXLogError("Failed to open %s", [mailFile stringValue]);
  34.  
  35.    return self;
  36. }
  37.  
  38. - free
  39. {
  40.    int x,cnt;
  41.  
  42.    fclose(fp);
  43.  
  44.    unlink([mailFile stringValue]);         /* unlink the mail file */
  45.    unlink([attachFile stringValue]);         /* unlink the compressed tar file */
  46.  
  47.    chdir([tmpDir stringValue]);             /* clean up the enclosure directory */
  48.    unlink("index.rtf");
  49.  
  50.    for(x = 0, cnt = [attachments count]; x < cnt; x++)
  51.        unlink([[attachments objectAt: x] basename]);
  52.  
  53.    chdir([NXApp appDirectory]);
  54.    rmdir([tmpDir stringValue]);             /* remove it */
  55.  
  56.    [tmpDir free];
  57.    [attachFile free];
  58.    return [super free];
  59. }
  60.    
  61. - buildBody
  62. {
  63.    id aFile;
  64.    struct stat sbuf;
  65.    int indexFile, cnt, x;
  66.    char c;
  67.    char attachString[3 * FILENAME_MAX], resultName[FILENAME_MAX];
  68.  
  69.    [super buildBody];
  70.  
  71.    indexFile = open("index.rtf",O_RDWR|O_CREAT,0600);
  72.    if(indexFile == -1)
  73.    {
  74.       NXLogError("Could not create index.rtf\n");
  75.       return nil;
  76.    }
  77.  
  78.    write(indexFile, rtfHeader, (size_t)strlen(rtfHeader));
  79.                          /* SUCKS - FIX LATER */
  80.  
  81.    for(x = 0, cnt = [body strlen]; x < cnt; x++)
  82.    {
  83.       c = *(char*)[body elementAt: x];
  84.       switch(c)
  85.       {
  86.        case '\134':
  87.           write(indexFile,"\134\134",2);
  88.           break; 
  89.        case '\n':
  90.           write(indexFile,"\134\n",2);
  91.           break; 
  92.        default:
  93.           write(indexFile,&c,1);
  94.           break;
  95.        }
  96.    }
  97.    
  98.    for(x = 0, cnt = [attachments count]; x < cnt; x++)
  99.    {
  100.       aFile = [attachments objectAt: x];
  101.       sprintf(attachString, templateAttach, [aFile basename]);
  102.       write(indexFile, attachString, (size_t)strlen(attachString));
  103.       symlink([aFile stringValue],[aFile basename]);
  104.    }
  105.    
  106.    write(indexFile,"}\n",2);
  107.    close(indexFile);
  108.                          /* build compressed tar file */
  109.    sprintf(attachString,tarCmd,[tmpDir stringValue], [attachFile stringValue]);
  110.    system(attachString);
  111.  
  112.    sprintf(resultName,".tar.%d%s", getpid(), [attachFile basename]);
  113.  
  114.                          /* update envelope */
  115.    stat([attachFile stringValue],&sbuf);
  116.    sprintf(attachString,envelopeAddition,resultName,sbuf.st_size);
  117.    fwrite(attachString,1,strlen(attachString),fp);
  118.    fwrite("\n",1,1,fp);
  119.    
  120.                          /* uueencode and append */
  121.    uuencode(resultName, [attachFile stringValue], fp, 0666);
  122.    return self;
  123. }
  124.  
  125. + (BOOL)supportsAttachments
  126. {
  127.    return YES;
  128. }
  129.  
  130.  
  131. @end
  132.